package com.jf.identity.util;

import com.jf.identity.dao.vis.OrgCodeDao;
import com.jf.identity.entity.OrgCode;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by x on 2018/5/18.
 */
@Component
public class OrgCodeCatchUtil {
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;
    @Resource(name = "redisTemplate")
    private ListOperations<String, Object> listOperations;
    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> valueOperations;
    @Resource
    private OrgCodeDao orgCodeDao;
    public Map<String,String> getOrgCodeCatch(){
        String key = "cdm_orgCode";
        Map<String,String> map;
        if (redisTemplate.hasKey(key)) {
            map = (Map<String,String>) valueOperations.get(key);

        } else {
            List<OrgCode> orgCodes = orgCodeDao.getAllOrgCode();
            map = new HashMap<>();
            for (OrgCode o:orgCodes) {
                map.put(o.getOrgCode(),o.getOrgIdenCode());
            }
            valueOperations.set(key,map, RedisConstant.M_FREQUENCY_TIME_OUT, TimeUnit.SECONDS);
        }
        return map;
    }
}
